package de.slothsoft.commons.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.text.MessageFormat;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.slothsoft.commons.SlothsoftConstants;
import de.slothsoft.commons.res.CommonImages;
import de.slothsoft.commons.res.CommonMessages;

public class AboutPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private static final String HTML_NEW_LINE = "<br>"; //$NON-NLS-1$
	private static final String HTML_START = "<html>"; //$NON-NLS-1$
	private static final String HTML_END = "</html>"; //$NON-NLS-1$
	private static final String HTML_LINK = "<a href='{0}'>{1}</a>"; //$NON-NLS-1$

	private static final int BORDER = 5;

	private final AboutInfo info;
	private JLabel imageLabel;
	private JLabel titleLabel;
	private JLabel infoLabel;

	public AboutPanel(AboutInfo info) {
		this.info = info;
		createMainPanel(this);
	}

	protected void createMainPanel(JPanel mainPanel) {
		mainPanel.setLayout(new BorderLayout());
		mainPanel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER));

		// icon

		JPanel imagePanel = new JPanel();
		imagePanel.setLayout(new BorderLayout());
		this.imageLabel = new JLabel();
		imagePanel.add(this.imageLabel, BorderLayout.NORTH);
		imagePanel.add(new JLabel(), BorderLayout.CENTER);
		mainPanel.add(imagePanel, BorderLayout.WEST);

		// title and info

		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());
		rightPanel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER));

		this.titleLabel = new JLabel();
		this.titleLabel.setFont(this.titleLabel.getFont()
				.deriveFont(Font.BOLD, this.titleLabel.getFont().getSize() + 4));
		this.infoLabel = new JLabel();
		this.infoLabel.setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER));
		rightPanel.add(this.titleLabel, BorderLayout.NORTH);
		rightPanel.add(this.infoLabel, BorderLayout.CENTER);
		mainPanel.add(rightPanel, BorderLayout.CENTER);

		fillPanel();
		calculatePreferredSize(imagePanel, rightPanel);
	}

	private void fillPanel() {
		this.titleLabel.setText(this.info.getApplicationTitle());
		this.infoLabel.setText(createInfoText());
		this.imageLabel.setIcon(this.info.getAboutIcon());

		if (this.info.getAboutIcon() == null) {
			this.imageLabel.setBorder(null);
		} else {
			this.imageLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		}

	}

	private void calculatePreferredSize(JPanel imagePanel, JPanel rightPanel) {
		int width = imagePanel.getPreferredSize().width + rightPanel.getPreferredSize().width;
		int height = Math.max(imagePanel.getPreferredSize().height, rightPanel.getPreferredSize().height);
		setPreferredSize(new Dimension(width, height));
		System.out.println(getPreferredSize());
	}

	private static String fill(String key, String... arguments) {
		return MessageFormat.format(CommonMessages.getString(key), (Object[]) arguments);
	}

	private String createInfoText() {
		String test;
		StringBuilder sb = new StringBuilder();

		sb.append(HTML_START);

		test = this.info.getVersion();
		if (test != null) {
			sb.append(fill("AboutDialog.version", test)); //$NON-NLS-1$
			sb.append(HTML_NEW_LINE);
			sb.append(HTML_NEW_LINE);
		}

		test = this.info.getCopyrightBy();
		if (test != null) {
			sb.append(fill("AboutDialog.copyright", test)); //$NON-NLS-1$

			String from = this.info.getCopyrightFrom();
			String to = this.info.getCopyrightTo();
			if (from != null) {
				sb.append(" ");//$NON-NLS-1$
				if (to != null && !to.equals(from)) {
					sb.append(fill("AboutDialog.copyrightTime", from, to)); //$NON-NLS-1$ 
				} else {
					sb.append(fill("AboutDialog.copyrightTimeSingle", from)); //$NON-NLS-1$ 
				}
			}
			sb.append(HTML_NEW_LINE);
			sb.append(HTML_NEW_LINE);
		}

		test = this.info.getHomepage();
		if (test != null) {
			sb.append(MessageFormat.format(HTML_LINK, test, test));
			sb.append(HTML_NEW_LINE);
			sb.append(HTML_NEW_LINE);
		}
		test = this.info.getComment();
		if (test != null) {
			sb.append(test);
		}
		sb.append(HTML_END);

		return sb.toString();
	}

	/*
	 * 
	 */

	public static class AboutInfo {

		private Image dialogImage = CommonImages.get(CommonImages.TOOLBAR_ABOUT).getImage();
		private Icon aboutIcon = CommonImages.get(CommonImages.SLOTHSOFT_ABOUT);
		private String applicationTitle;
		private String version;
		private String homepage = SlothsoftConstants.HOMEPAGE;
		private String copyrightBy = SlothsoftConstants.COPYRIGHT_BY;
		private String copyrightFrom;
		private String copyrightTo;
		private String comment;

		public AboutInfo(String applicationTitle) {
			this(applicationTitle, null);
		}

		public AboutInfo(String applicationTitle, String version) {
			this(applicationTitle, version, null, null);
		}

		public AboutInfo(String applicationTitle, String version, String copyrightFrom, String copyrightTo) {
			this.applicationTitle = applicationTitle;
			this.version = version;
			this.copyrightFrom = copyrightFrom;
			this.copyrightTo = copyrightTo;
		}

		public Icon getAboutIcon() {
			return this.aboutIcon;
		}

		public void setAboutIcon(Icon aboutIcon) {
			this.aboutIcon = aboutIcon;
		}

		public String getApplicationTitle() {
			return this.applicationTitle;
		}

		public void setApplicationTitle(String applicationTitle) {
			this.applicationTitle = applicationTitle;
		}

		public String getVersion() {
			return this.version;
		}

		public void setVersion(String version) {
			this.version = version;
		}

		public String getHomepage() {
			return this.homepage;
		}

		public void setHomepage(String homepage) {
			this.homepage = homepage;
		}

		public String getCopyrightBy() {
			return this.copyrightBy;
		}

		public void setCopyrightBy(String copyrightBy) {
			this.copyrightBy = copyrightBy;
		}

		public String getCopyrightFrom() {
			return this.copyrightFrom;
		}

		public void setCopyrightFrom(String copyrightFrom) {
			this.copyrightFrom = copyrightFrom;
		}

		public String getCopyrightTo() {
			return this.copyrightTo;
		}

		public void setCopyrightTo(String copyrightTo) {
			this.copyrightTo = copyrightTo;
		}

		public Image getDialogImage() {
			return this.dialogImage;
		}

		public void setDialogImage(Image dialogIcon) {
			this.dialogImage = dialogIcon;
		}

		public String getComment() {
			return this.comment;
		}

		public void setComment(String comment) {
			this.comment = comment;
		}

	}
}
